<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of uploadfile
 *
 * @author win7
 */
class fileupload{
    //put your code here
    var $CI='';
    var $config =array();
    public function __construct() {
     $this->CI=&get_instance();
     $this->config['upload_path'] = './uploads/';
     $this->config['allowed_types'] = 'gif|jpg|png';
     $this->config['max_size']    = '100';
     $this->config['max_width']  = '1024';
     $this->config['max_height']  = '768';
    }
    
    function valid_upload($fieldname='',$valid=''){
        $CI=&get_instance();
        

        if (!$CI->upload->valid_upload($fieldname,$valid))
        {
            return false;
        } else {
            return true;
        }
    }
    
    
    
     function upload_file($url='',$fieldname,$oldfile='',$configs=array()){
        if(empty($_FILES[$fieldname]['tmp_name'])){
           return $oldfile; 
        }else{
         
         $CI=&get_instance();
        $config=array();
        $config['upload_path'] = AVATAR_PATH;
        $config['file_name'] = time();
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']    = '';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
        //$this->load->library('upload',$config);
        foreach ($configs as $key => $value) {
         $config[$key]  = $value;   
        }

        $CI->upload->initialize($config);

        if (!$CI->upload->do_upload($fieldname))
        {
        
            $CI->themes->set_message(array('url' =>$url, 'type' => 'error', 'message' => $CI->upload->display_errors()));
            return false;
        } else {
            $upload_data=$CI->upload->data();
            if(!empty($oldfile)){
                @unlink($config['upload_path'].$oldfile); 
            }
            return $upload_data['file_name'];

        }
       }
    } 
}

?>
